package com.rxjava.simplejust;

/**
 * 被观察者，用来生产发送事件；
 */
public class Observable<T> {

    private OnSubscribe<T> onSubscribe;

    private Observable(OnSubscribe<T> onSubscribe) {
    	//onSubscribe -> OnSubscribeFromArray对象
        this.onSubscribe = onSubscribe;
    }

    public static <T> Observable<T> just(final T value) {
        return create(new OnSubscribe<T>() {
            @Override
            public void call(Subscriber<? super T> subscriber) {
                subscriber.onNext(value);
            }
        });
    }

    public static <T> Observable<T> just(final T t1, final T t2) {
        return from((T[]) new Object[]{t1, t2});
    }

    public static <T> Observable<T> from(T[] array) {
    	//array -> new Integer[]{4, 5}
        return create(new OnSubscribeFromArray<T>(array));
    }

    public final void subscribe(Subscriber<? super T> subscriber) {
    	//subscriber -> System.out.println(integer);
        onSubscribe.call(subscriber);
    }

    public static <T> Observable<T> create(OnSubscribe<T> onSubscribe) {
    	//onSubscribe -> OnSubscribeFromArray对象
        return new Observable<>(onSubscribe);
    }

}